package ru.usu.gv.utils.octree;

import ru.usu.gv.utils.RandomUtilities;

import javax.vecmath.Point3d;

import java.util.HashMap;
import java.util.Map;

/**
 * @author spupyrev
 * 05.11.2008
 */
public class OctreeTest
{
	private static IOctreeNode createNode()
	{
		return new IOctreeNode()
		{
			public double getWeight()
			{
				return 1;
			}
		};
	}

	public static void main(String argc[])
	{
		int MAX_NODES = 10;
		Map<IOctreeNode, double[]> nodes = new HashMap<IOctreeNode, double[]>();
		for (int i = 0; i < MAX_NODES; i++)
		{
			IOctreeNode node = createNode();
			Point3d p = RandomUtilities.generateRandomPoint();
			p.set((double) i / MAX_NODES, (double) i / MAX_NODES, 0);
			nodes.put(node, new double[] { p.x, p.y, p.z });
		}

		double[] minPos = { Double.MAX_VALUE, Double.MAX_VALUE, Double.MAX_VALUE };
		double[] maxPos = { -Double.MAX_VALUE, -Double.MAX_VALUE, -Double.MAX_VALUE };
		for (IOctreeNode node : nodes.keySet())
		{
			double[] position = nodes.get(node);
			for (int d = 0; d < position.length; d++)
			{
				minPos[d] = Math.min(position[d], minPos[d]);
				maxPos[d] = Math.max(position[d], maxPos[d]);
			}
		}
		// provide additional space for moving nodes
		for (int d = 0; d < 3; d++)
		{
			double posDiff = maxPos[d] - minPos[d];
			maxPos[d] += posDiff / 2;
			minPos[d] -= posDiff / 2;
		}

		long startTime = System.currentTimeMillis();
		Octree octree = new Octree(null, new double[3], minPos, maxPos);

		for (IOctreeNode node : nodes.keySet())
		{
			double[] position = nodes.get(node);
			octree.addNode(node, new Point3d(position));
			System.out.println(octree.getHeight());
		}

		for (IOctreeNode node : nodes.keySet())
		{
			double[] position = nodes.get(node);
			octree.removeNode(node, new Point3d(position));
			System.out.println(octree.getHeight());
		}

		long endTime = System.currentTimeMillis();
		System.out.println("Finished in " + (endTime - startTime) / 1000.0 + " seconds");
	}
}
